/**
 * 
 */
package edu.cmu.cs.webapp.ioms.ui.providers.user;

import java.text.DateFormat;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import edu.cmu.cs.webapp.ioms.models.Client;
import edu.cmu.cs.webapp.ioms.models.User;
import edu.cmu.cs.webapp.ioms.utils.IOMSImageHelper;
import edu.cmu.cs.webapp.ioms.utils.IOMSImages;

/**
 * @author Christian
 * 
 */
public class UsersTableViewerLabelProvider extends LabelProvider implements
		ITableLabelProvider {
	// table column index
	public static final int USER_ID = 0;
	public static final int LOGIN = 1;
	public static final int NAME = 2;
	public static final int ROLE = 3;
	public static final int DEPARTMENT = 4;
	public static final int LAST_LOGIN = 5;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
	 * .Object, int)
	 */
	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		Image img = null;

		if (element instanceof User) {
			User user = (User) element;
			if (user.getRole().equals("Staff")) {
				img = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_ROLE_STAFF);
			} else if (user.getRole().equals("Administrator")) {
				img = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_ROLE_ADMIN);
			}

			if (columnIndex == USER_ID) {
				return img;
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang
	 * .Object, int)
	 */
	@Override
	public String getColumnText(Object element, int columnIndex) {
		if (element instanceof User) {
			User user = (User) element;
			switch (columnIndex) {
			case USER_ID:
				return "#" + user.getUser_id();
			case LOGIN:
				return user.getLogin();
			case NAME:
				return user.getName();
			case ROLE:
				return user.getRole();
			case DEPARTMENT:
				return user.getDepartment();
			case LAST_LOGIN:
				return user.getLast_login() == null ? "" : DateFormat
						.getDateTimeInstance().format(user.getLast_login());
			default:
				return "";
			}
		}

		return "";

	}
}
